package cn.raven.pet.controller;

import cn.raven.basic.annotation.PreAuthorize;
import cn.raven.basic.exception.BusinessException;
import cn.raven.basic.utils.LoginContext;
import cn.raven.org.domain.Employee;
import cn.raven.pet.dto.HandlerMsgDto;
import cn.raven.pet.service.ISearchMasterMsgService;
import cn.raven.pet.domain.SearchMasterMsg;
import cn.raven.pet.query.SearchMasterMsgQuery;
import cn.raven.basic.utils.AjaxResult;
import cn.raven.basic.utils.PageList;
import cn.raven.user.domain.Logininfo;
import cn.raven.user.domain.User;
import cn.raven.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;
    @Autowired
    private IUserService userService;


    /**
     * 保存和修改公用的
     *
     * @param searchMasterMsg 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @PreAuthorize(name = "保存searchMasterMsg", sn = "searchMasterMsg:save")
    public AjaxResult save(@RequestBody SearchMasterMsg searchMasterMsg) {
        try {
            if (searchMasterMsg.getId() != null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.add(searchMasterMsg);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！" + e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    @PreAuthorize(name = "删除searchMasterMsg", sn = "searchMasterMsg:delete")
    public AjaxResult del(@PathVariable("id") Long id) {
        try {
            searchMasterMsgService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！" + e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    @PreAuthorize(name = "id查询searchMasterMsg", sn = "searchMasterMsg:id")
    public SearchMasterMsg findById(@PathVariable("id") Long id) {
        return searchMasterMsgService.findOne(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping()
    public List<SearchMasterMsg> findAll() {

        return searchMasterMsgService.findAll();
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    @PreAuthorize(name = "searchMasterMsg列表", sn = "searchMasterMsg:list")
    public PageList<SearchMasterMsg> queryData(@RequestBody SearchMasterMsgQuery query) {
        return searchMasterMsgService.queryData(query);
    }

    /** 
    * @Title: publish
    * @Description: 发布寻主消息
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/19 18:14  
    * @Parameters: [searchMasterMsg, request]
    * @Return cn.raven.basic.utils.AjaxResult
      */
    @PostMapping("/publish")
    @PreAuthorize(name = "发布寻主消息", sn = "searchMasterMsg:publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request) {
        try {
            User user = LoginContext.getUser(request);
            searchMasterMsg.setUserId(user.getId());
            searchMasterMsgService.publish(searchMasterMsg);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！" + e.getMessage());
        }
    }

    /**
    * @Title: toAudit
    * @Description: 待审核
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/20 14:44
    * @Parameters: [searchMasterMsgQuery]
    * @Return cn.raven.basic.utils.PageList<cn.raven.pet.domain.SearchMasterMsg>
      */
    @PostMapping("/toAudit")
    public PageList<SearchMasterMsg> toAudit(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery) {
        // 1.设置寻主消息状态为待审核
        searchMasterMsgQuery.setState(0);
        // 2.查询待审核的寻主消息寻主消息
        return  searchMasterMsgService.queryData(searchMasterMsgQuery);
    }

    /** 
    * @Title: toHandle
    * @Description: 待处理
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/20 14:49  
    * @Parameters: [searchMasterMsgQuery]
    * @Return cn.raven.basic.utils.PageList<cn.raven.pet.domain.SearchMasterMsg>
      */
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery,HttpServletRequest request) {
        // 1.设置寻主消息状态为待处理
        searchMasterMsgQuery.setState(1);
        //shopid为当前管理员的shopid
        Employee employee = LoginContext.getEmployee(request);
        searchMasterMsgQuery.setShopId(employee.getShopId());
        // 2.查询待处理的寻主消息寻主消息
        return  searchMasterMsgService.queryData(searchMasterMsgQuery);
    }

    /**
    * @Title: pool
    * @Description: 寻主池
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/20 14:53
    * @Parameters: [searchMasterMsgQuery, request]
    * @Return cn.raven.basic.utils.PageList<cn.raven.pet.domain.SearchMasterMsg>
      */
    @PostMapping("/pool")
    public PageList<SearchMasterMsg> pool(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery,HttpServletRequest request) {
        // 1.设置寻主消息状态为审核通过
        searchMasterMsgQuery.setState(1);
        // 2.查询审核通过的寻主消息寻主消息
        return  searchMasterMsgService.queryData(searchMasterMsgQuery);
    }

    /**
    * @Title: finish
    * @Description: 已完成
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/20 14:54
    * @Parameters: [searchMasterMsgQuery, request]
    * @Return cn.raven.basic.utils.PageList<cn.raven.pet.domain.SearchMasterMsg>
      */
    @PostMapping("/finish")
    public PageList<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery,HttpServletRequest request) {
        // 1.设置寻主消息状态为已完成
        searchMasterMsgQuery.setState(2);
        // 2.查询已完成的寻主消息寻主消息
        return  searchMasterMsgService.queryData(searchMasterMsgQuery);
    }
    /**
    * @Title: userSearchMasterMsg
    * @Description: 用户个人中心寻主消息
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/20 17:08
    * @Parameters: [searchMasterMsgQuery, request]
    * @Return cn.raven.basic.utils.PageList<cn.raven.pet.domain.SearchMasterMsg>
      */
    @PostMapping("/user")
    public PageList<SearchMasterMsg> userSearchMasterMsg(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery, HttpServletRequest request) {
        // 1.设置当前登录用户Id
        User user = LoginContext.getUser(request);
        searchMasterMsgQuery.setUserId(user.getId());
        // 2.查询待审核的寻主消息寻主消息
        return  searchMasterMsgService.queryData(searchMasterMsgQuery);
    }

    /** 
    * @Title: accept
    * @Description: 宠物待处理接单
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/20 17:14
    * @Parameters: [handlerMsgDto]
    * @Return cn.raven.basic.utils.AjaxResult
      */
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody HandlerMsgDto handlerMsgDto){
        try {
            searchMasterMsgService.accept(handlerMsgDto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage()).setSuccess(false);
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙,请骚后重试").setSuccess(false);
        }
    }

    /**
    * @Title: reject
    * @Description: 拒单
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/20 18:50
    * @Parameters: [id]
    * @Return cn.raven.basic.utils.AjaxResult
      */
    @PostMapping("/reject/{id}")
    @PreAuthorize(name = "宠物上架",sn = "pet:onsale")
    public AjaxResult reject(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.reject(id);
            return AjaxResult.me();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage()).setSuccess(false);
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙,请骚后重试").setSuccess(false);
        }
    }
}
